<?php
declare (strict_types=1);

namespace app\course\logic\base\admin;

use app\admin\logic\system\Log;
use app\common\exception\LogicException;
use app\course\model\base\admin\Course as BaseCourse;
use app\course\model\classes\admin\ClassesLink;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;

/**
 * 课程总接口-不区分课程类型
 */
class Course extends BaseCourse
{

    /**
     * 是否为班级课
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/2
     *
     * @param $id
     * @return int
     */
    public function isClasses($id)
    {
        $data = $this
            ->where('id', $id)
            ->where('course_type', 4)
            ->findOrEmpty();

        return $data->isEmpty() ? 0 : 1;
    }

    /**
     * 复制课程
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/2
     *
     * @param int $courseId
     */
    public function copyCourseInfo(int $courseId)
    {
        $info = $this->where('id', $courseId)->findOrEmpty();
        if ($info->isEmpty()) {
            throw new LogicException('课程信息不存在');
        }

        $data = $info->toArray();
        unset($data['id']);
        $time                  = Request::time();
        $data['create_time']   = $time;
        $data['update_time']   = $time;
        $data['listing_time']  = $time;
        $data['admin_user_id'] = request()->admin_user_id;

        $ret = $this->create($data);
        if ($ret->isEmpty()) {
            throw new LogicException('复制失败');
        }
    }

    /**
     * 课程是否已经更新完成
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/9/24
     *
     * @param int $courseId
     * @return int
     */
    public function isFinished(int $courseId)
    {
        return $this->where('id', $courseId)->value('update_status') == 1 ? 0 : 1;
    }

    /**
     * 是否被引用到了班级课程
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/12
     *
     * @param int  $courseId
     * @param bool $getTitle
     * @return int
     */
    public static function isLinkToClasses(int $courseId, bool $getTitle = true)
    {
        $classesId = app(ClassesLink::class)->where('course_id', $courseId)->value('classes_id');
        if ($classesId && $getTitle) {
            return app(Course::class)->where('id', $classesId)->value('title');
        }

        return boolval($classesId);
    }

    /**
     * 批量设置点播课程字段值
     *
     * @param string $field
     * @param        $value
     * @return Course
     */
    public function batchUpdatedField(string $field, $value)
    {
        $where = ['course_type' => 1, 'mhm_id' => request()->mhm_id];

        $ret = Course::update([$field => $value], $where);
        if ($ret && $field === 'onhook_type') {
            // 防挂机批量设置日志
            $courseOnhookType = [
                '全部取消',
                '全部为弹窗验证',
                '全部为弹题验证',
                '全部为人脸验证'
            ];
            Log::write('批量设置课程防挂机验证方式为【' . $courseOnhookType[$value] ?? '未知' . '】', 'course', 'update', true);
        }

        return $ret;
    }

    /**
     * getCourses
     *
     * @param array $ids
     * @param       $field
     * @return Course[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getCourses(array $ids, $field = '*')
    {
        return $this->with(['cover_url'])->whereIn('id', $ids)->field($field)->select();
    }

    /**
     * 批量更新数据
     *
     * @param array  $ids
     * @param string $field
     * @param        $value
     */
    public function batchUpdated(array $ids, string $field, $value)
    {
        $ret = BaseCourse::update([$field => $value], [['id', 'in', $ids]]);
        if (!$ret) {
            throw new LogicException('批量操作失败');
        }
    }
}
